package yxy.game.pm2.module.battle;

import yxy.cherry.battle.module.PropertyData;
import yxy.cherry.battle.module.PropertyType;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.herodata;

import java.util.ArrayList;

import static yxy.cherry.battle.Battle.battleLogger;

public class FighterModule {

    private int heroId;// 数据id
    private int level;// 等级
    private int stage;// 进阶
    private int starlv;// 星级
    private int anger;// 初始怒气
    private InitHp initHp;// 血量控制
    private ArrayList<String> skills;// 附加的技能

    private PropertyModule property;
    private long totalPower;//总战力（地图怪物总战力）

    public long getTotalPower() {
        return totalPower;
    }

    public void setTotalPower(long totalPower) {
        this.totalPower = totalPower;
    }

    public int getHeroId() {
        return heroId;
    }

    public void setHeroId(int heroId) {
        this.heroId = heroId;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getStage() {
        return stage;
    }

    public void setStage(int stage) {
        this.stage = stage;
    }

    public int getStarlv() {
        return starlv;
    }

    public void setStarlv(int starlv) {
        this.starlv = starlv;
    }

    public int getAnger() {
        return anger;
    }

    public void setAnger(int anger) {
        this.anger = anger;
    }

    public InitHp getInitHp() {
        return initHp;
    }

    public void setInitHp(InitHp initHp) {
        this.initHp = initHp;
    }

    public ArrayList<String> getSkills() {
        return skills;
    }

    public void setSkills(ArrayList<String> skills) {
        this.skills = skills;
    }

    public PropertyModule getProperty() {
        return property;
    }

    public void setProperty(PropertyModule property) {
        this.property = property;
    }

    // func===========================================================================
    public long hp() {
        if (initHp == null) {
            return life();
        }
        return initHp.handle(this, PropertyModule.propertyData(getProperty()));
    }

    public long life() {
        PropertyData propertyData = PropertyModule.propertyData(getProperty());
//		BattleHandle.packProperty属性增幅(propertyData);
        return propertyData.getValue(PropertyType.P_生命);
    }

    static public FighterModule create(int heroId, int level, int stage, int star, int anger) {
        return create(heroId, level, stage, star, anger, null);
    }

    static public FighterModule create(int heroId, int level, int stage, int star, int anger, InitHp initHp) {
        try {
            herodata _herodata = DataCenter.getData(heroId, herodata.class);
            PropertyData propertyData = BattleHandle.packProperty武将基础属性(_herodata, level, stage);
            FighterModule fighterModule = new FighterModule();
            fighterModule.setHeroId(heroId);
            fighterModule.setLevel(level);
            fighterModule.setStage(stage);
            fighterModule.setStarlv(star);
            fighterModule.setAnger(anger + _herodata.getInitialanger());

            if (initHp != null) {
                fighterModule.setInitHp(initHp);
            }

            fighterModule.setProperty(PropertyModule.create(propertyData));

            return fighterModule;
        } catch (Exception e) {
//			e.printStackTrace();
        }
        return null;
    }

    static public interface InitHp {
        /**
         * 处理武将初始血量 返回实际血量
         */
        public long handle(FighterModule fighterModule, PropertyData propertyData);
    }

    /**
     * 根据残血 设定武将初始血量
     */
    static public class InitHpResidual implements InitHp {
        private long hp;// 初始血量
        private long residual;// 残血

        public InitHpResidual(long hp, long residual) {
            this.hp = hp;
            this.residual = residual;
        }

        public long getHp() {
            return hp;
        }

        public void setHp(long hp) {
            this.hp = hp;
        }

        public long getResidual() {
            return residual;
        }

        public void setResidual(long residual) {
            this.residual = residual;
        }

        @Override
        public long handle(FighterModule fighterModule, PropertyData propertyData) {
            if (getHp() > 0) {
                propertyData.setValue(PropertyType.P_生命, getHp());
            }
            return getResidual();
        }

    }

    /**
     * 根据损失比例 设定武将初始血量
     */
    static public class InitHpLossRatio implements InitHp {
        private int ratio;

        public InitHpLossRatio(int ratio) {
            this.ratio = ratio;
        }

        public int getRatio() {
            return ratio;
        }

        public void setRatio(int ratio) {
            this.ratio = ratio;
        }

        @Override
        public long handle(FighterModule fighterModule, PropertyData propertyData) {
//			BattleHandle.packProperty属性增幅(propertyData);
            long hp = propertyData.getValue(PropertyType.P_生命);
            hp = hp * ratio / 10000;
            return hp < 0 ? 0 : hp;
        }

    }

}
